home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / MasteringDetails / EmpController.m < prev    next >
Encoding:
Text File  |  1995-02-18  |  2.6 KB  |  71 lines

  1.  
  2. #import "EmpController.h"
  3. #import <eointerface/eointerface.h>
  4. #import "ValueForKey.h"
  5. #import "SelectionInsertionAssociation.h"
  6.  
  7. @implementation EmpController
  8.  
  9. - appDidInit:sender
  10. {
  11.     // add custom association that inserts and deletes objects in the
  12.     // empProjectsController based on the selection in the allProjectsController
  13.     [empProjectsController addAssociation:[[SelectionInsertionAssociation alloc] initWithController:empProjectsController key:nil destination:allProjectsController]];
  14.  
  15.     // add custom association that inserts and deletes objects in the
  16.     // empDeptsController based on the selection in the allDeptsController
  17.     [empDeptsController addAssociation:[[SelectionInsertionAssociation alloc] initWithController:empDeptsController key:nil destination:allDeptsController]];
  18.  
  19.     // Fetch the list of all projects and all departments for our pick lists.
  20.     // A real app would probably cache these list so it wouldn't have to
  21.     // requery the database each time a new window is opened.
  22.     [allProjectsController fetch:nil];
  23.     [allDeptsController fetch:nil];
  24.  
  25.     // Set a controller in memory sort for first name and last name
  26.     // and fetch all of the employees
  27.     [empController setSortOrdering:[NSArray arrayWithObjects:
  28.         [EOKeySortOrdering keyOrderingWithKey:@"last_name" ordering:NSOrderedAscending],
  29.         [EOKeySortOrdering keyOrderingWithKey:@"first_name" ordering:NSOrderedAscending], nil]];
  30.     [empController fetch:nil];
  31.  
  32.     return self;
  33. }
  34.  
  35. - insert:sender
  36. {
  37.     // Add a new employee
  38.     // In addition to inserting the employee itself, we need to insert
  39.     // in any detail controller that represent mandatory to-one relationships.
  40.     // Possible Enhansement: make the Savvy controller propagate inserts to
  41.     // detail controllers automatically.
  42.     [empController insert:self];
  43.     [quoteController insert:self];
  44.     [photoController insert:self];
  45.     // default the department for the new employee to the first department in the list 
  46.     [empDeptsController insertObject:[[allDeptsController allObjects] objectAtIndex:0] atIndex:0];
  47.     return self;
  48. }
  49.  
  50. - (BOOL)controllerWillDiscardOperations:(EOController *)controller
  51. {
  52.     // Application policy: if they fetch or change selection,
  53.     // automatically save the current employee data.
  54.     return [self save:nil] ? YES : NO;
  55. }
  56.  
  57.  
  58. - save:sender
  59. {
  60.     [empController saveToDataSource];
  61.  
  62.     // Updates to controllers (like our department) may have changed property
  63.     // values that we display in the master.
  64.     // Re-sort in case the save changed the sort attributes
  65.     [empController resort];
  66.  
  67.     return self;
  68. }
  69.  
  70. @end
  71.